
import { _decorator, Component, Node,find, Label, log, randomRangeInt } from 'cc';
import { GameConfig } from '../gameConfig';
import { MVec2 } from '../gamecore/MVect2';
import { ResultMsg } from '../gamecore/resultMsg';
import { FogManager } from '../ui/fogManager';
const { ccclass, property } = _decorator;
const io = (window as any).io || {};

/**
 * Predefined variables
 * Name = TestSocketioClien
 * DateTime = Sun Mar 20 2022 15:13:09 GMT+0800 (中国标准时间)
 * Author = mdkmarui
 * FileBasename = testSocketioClien.ts
 * FileBasenameNoExtension = testSocketioClien
 * URL = db://assets/secripts/netIo/testSocketioClien.ts
 * ManualUrl = https://docs.cocos.com/creator/3.3/manual/zh/
 *
 */
 
@ccclass('TestSocketioClien')
export class TestSocketioClien extends Component {
    @property(Node)
    msgLab : Node = null;
    @property(Node)
    fogs: Node = null;
    fogManager: FogManager = null;

    lable:Label = null;

    private socket: any;
    
    private static _instance: TestSocketioClien;

    onLoad () {
        this.lable=this.msgLab.getComponent(Label);
        this.fogManager = this.fogs.getComponent(FogManager);
    }

    static get instance() {
        if (TestSocketioClien._instance == null) {
            TestSocketioClien._instance = find("Canvas/FOG_GO_IO").getComponent(TestSocketioClien);
        }
        if (TestSocketioClien._instance == null) {
            let n: Node = new Node();
            n.name = "FOG_GO_IO";
            n.parent = find("Canvas");
            TestSocketioClien._instance = n.addComponent(TestSocketioClien);
        }
        if (TestSocketioClien._instance.socket == null) {
            let rn = randomRangeInt(1,100);
            let socket = io.connect('http://127.0.0.1:8000/?username=小明'+rn+'&password=123456');
            socket.on('gameStart',function(r) {
                log(r);
                GameConfig.instance.gameSize = r.N;
                GameConfig.instance.points = r.Points;
                GameConfig.instance.myColor = r.YourColor;
                GameConfig.instance.pieceColor = r.PieceColor;
                this.fogManager.fogMetaArr = r.Masks;
                this.fogManager.init();
              });




            TestSocketioClien._instance.socket = socket;
        }
        
        return TestSocketioClien._instance;
    }

    croom(rid:string) {
        // let lable : Label = this.msgLab.getComponent(Label);
        this.socket.emit('createRoom', rid, function (data) {
            if (data) {
                this.lable.string = ""+data;
                log(data)
            }
          });
    }

    jroom(rid:string) {
        // let lable : Label = this.msgLab.getComponent(Label);
        this.socket.emit('joinRoom', rid, function (data) {
            if (data) {
                this.lable.string = ""+data;
                log(data)
            }
          });
    }

    tryPlay(v:MVec2,rs:(data: ResultMsg) => void) {
        this.socket.emit('tryPlay', v, rs);
    }

    play(v:MVec2, rs: (data: string) => void ) {
        this.socket.emit('play', v, rs);
    }

}

/**
 * [1] Class member could be defined like this.
 * [2] Use `property` decorator if your want the member to be serializable.
 * [3] Your initialization goes here.
 * [4] Your update function goes here.
 *
 * Learn more about scripting: https://docs.cocos.com/creator/3.3/manual/zh/scripting/
 * Learn more about CCClass: https://docs.cocos.com/creator/3.3/manual/zh/scripting/ccclass.html
 * Learn more about life-cycle callbacks: https://docs.cocos.com/creator/3.3/manual/zh/scripting/life-cycle-callbacks.html
 */
